from django.shortcuts import render
from django.shortcuts import get_object_or_404
from .models import Article, Column


# Create your views here.
def index(request):
    column_list = Column.objects.order_by('-update_date')
    updated_article_list = Article.objects.order_by('-update_time')[:10]
    return render(request, 'MyBlog/index.html', {'column_list': column_list,
                                                 'updated_article_list': updated_article_list})


def blog_detail(request, article_id):
    article_list = Article.objects.order_by('-pub_date')[:10]
    updated_article_list = Article.objects.order_by('-update_time')[:10]
    blog_article = get_object_or_404(Article, pk=article_id)
    blog_article.read_times += 1
    blog_article.save()
    print(blog_article.read_times)
    return render(request, 'MyBlog/detail.html', {'blog_article': blog_article
                                                  , 'article_list': article_list,
                                                  'updated_article_list': updated_article_list})


def column_detail(request, column_id):
    column = get_object_or_404(Column, pk=column_id)
    updated_article_list = Article.objects.order_by('-update_time')[:10]
    article_list = list(Article.objects.filter(column_id=column_id))
    return render(request, 'MyBlog/column.html', {'column': column,
                                                  'article_list': article_list,
                                                  'updated_article_list': updated_article_list,
                                                  })
